<?php 
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');
$mm = new MessagingModel('Messaging');
$smsc= $mm->smsc();

function lookup($what='',$p1='',$p2=''){
    $l = new MessagingAction();
    return $l->lookup($what,$p1,$p2);
}
sajax_init();
//$sajax_debug_mode = 1;
sajax_export('lookup');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 

?>

<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<script type="text/javascript" src="<?=WEB_MODULE_DIR?>js/javascript.js"></script>

<script language="JavaScript" type="text/javascript">
<?php sajax_show_javascript();?> 

function checkmobile(msisdn)
{
    var re = new RegExp('^(09\\d{9}|222|214|258|2870)$')
    if (msisdn.value.match(re) ) {
        return true;
    }else{
        alert('Incorrect Number')
        return false;
    }
}

function prepload(skip){
  $('forload').innerHTML = loading;
  $('contactdiv').style.display='none';
  ClearOptions($('ListBox1'));
  if (!skip) ClearOptions($('ListBox2'));
}
function afterload(){
  $('forload').innerHTML = '';
  $('contactdiv').style.display='block';
}

function toggleSched(t){
  $('triger1').disabled = !t;      
}
function listrange(f,t){
    prepload(true)
    cat = ($('list_groups').checked) ? 'groups' : 'contacts';
    x_lookup(cat,f,t,function(nd){
        AddToOptionList(Json_eval(nd),$('ListBox1'),'id','name')
        afterload();
    });
}

    function update() {
       var old = $('counter').value;
       $('counter').value=$('message').value.length;
       if($('counter').value > 320 && old <= 320) {
         alert('Too much data in the text box!');
         if(document.styleSheets) {
           $('counter').style.fontWeight = 'bold';
           $('counter').style.color = '#ff0000'; } }
       else if($('counter').value <= limit && old > limit
	       && document.styleSheets ) {
           $('counter').style.fontWeight = 'normal';
           $('counter').style.color = '#000000'; } 
       }

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
        else
        countfield.value = maxlimit - field.value.length;
    }

    function CheckKey(e)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 60) { e.preventDefault(); }
    }
    function CheckCtrl(e,r)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 17) { document.Form1.send_Message.focus(); }
    }
    function verify(){
        if ($('message').value == ''){
            alert('Message cannot be blank!')
            return false;
        }else if ($('ListBox2').value.trim() == ''){
            alert('Contacts cannot be blank!')
            return false;
        }else if (!$('triger1').disabled && $('send_date').value == ''){
            alert('Please provide schedule date!')
            return false;
        }else if (!$('triger1').disabled && $('send_date').value != ''){
            $('send_date').disabled = false;
        }
    }
</script>

<form id="form" class="wufoo" method="post" onsubmit="return verify();">
<div id="divUpdatePanel" style="width: 760px;" class="container">
    <table border="0" width="90%">
        <tbody><tr valign="top">
            <td colspan="2" style="width: 52%;" align="left">
                <label style="color: rgb(109, 107, 108);">
                    Message</label>
                <span id="RequiredFieldValidator1" style="color: Red; visibility: hidden;"> *</span></td>
        </tr>
        <tr valign="top">
            <td style="width: 52%;" align="left">
                 <textarea name="message" rows="2" cols="20" id="message" onkeyup="textCounter($('message'),$('counter'),320);" onchange="textCounter($('message'),$('counter'),320);" onkeypress="CheckKey(event)" onkeydown="CheckCtrl(event,this)" style="height: 90px; width: 300px;"></textarea>
                    <div style="height: 10px;"></div>
                <div>
                    <label style="color: rgb(109, 107, 108);">
                        Message Count</label>
                    &nbsp;&nbsp;
                    <input type="hidden" id="utemp_id" name="details[utemp_id]" value="0"/>
                    <input name="counter" value="320" id="counter" disabled="disabled" style="width: 60px;" type="text">
                                      &nbsp;&nbsp;Insert Template&nbsp;&nbsp;<input type="button" name="revise" class="btnformat" value="..." 
                                        onClick="popUp('?module=<?=DEFAULT_MODULE?>&action=TemplateLookup'); return false;"></div>
            </td>
            <td style="width: 48%;" align="left">
                <label style="color: rgb(109, 107, 108);">
                    Sending Options</label>
                &nbsp;&nbsp;&nbsp;
                <input id="send_now" name="send" value="send_now" checked="checked" type="radio" onclick="toggleSched(false)"><label for="send_now">Now</label>
                &nbsp;&nbsp;&nbsp;
                <input id="send_sched" name="send" value="send_sched" onclick="toggleSched(true)" type="radio"><label for="send_sched">Schedule</label>
                <br>
                <br>
               		<input id='send_date' size=30 name="send_date" value="" disabled />
		            <input id='triger1' size=10 type="reset" value="..." disabled /> 
                <br>
                <br>
                <label style="color: rgb(109, 107, 108);"> SMSC:</label>
		          <?=show_select3('network','network', 'smsc',$smsc);?>
            </td>
        </tr>
        
    </tbody></table>
    <br>
        <div id="forload"></div>
    <br>

<div id="contactdiv">
    <table border="0" width="90%">
        <tbody><tr>
            <td colspan="3" align="left">
                <label style="color: rgb(109, 107, 108);">
                    Send To</label></td>
        </tr>
        <tr>
            <td style="width: 45%;" align="left">
                <select size="4" name="ListBox1" multiple="multiple" id="ListBox1" 
                        onDblClick="move($('ListBox1'),$('ListBox2'))"
                        style="height: 250px; width: 300px;"> 
                </select>
            </td>
            <td align="center">
                <input name="add" value="&gt;&gt;" id="add" type="button" onclick="move($('ListBox1'),$('ListBox2'))"><br>
                <br>
                <input name="remove" value="&lt;&lt;" id="remove" type="button" onclick="move($('ListBox2'),$('ListBox1'))">
            </td>
            <td style="width: 45%;" align="right">
                <select size="4" name="ListBox2[]" multiple="multiple" id="ListBox2"
                        onDblClick="move($('ListBox2'),$('ListBox1'))"
                        style="height: 250px; width: 300px;">
                </select>
            </td>
        </tr>
    </tbody></table>
    <table border="0" width="90%">
        <tbody><tr>
            <td align="left">
                <input id="list_groups" name="list_cat" value="list_groups" checked="checked" type="radio" onclick="listrange('a','z');;ClearOptions($('ListBox2'))">
                <label for="list_groups">Groups</label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input id="list_contacts" name="list_cat" value="list_contacts" type="radio" onclick="listrange('a','z');ClearOptions($('ListBox2'))">
                <label for="list_contacts">Contacts</label>
            </td>
            <td align="left">
                <span id="RequiredFieldValidator2" style="color: Red; visibility: hidden;">Required Field</span></td>
        </tr>
        <tr>
            <td style="height: 8px;" align="left">
                <input name="sort_all" value="All" id="sort_all" onclick="listrange('a','z')" type="button">
                <input name="sort_a2f" value="A-F" id="sort_a2f" onclick="listrange('a','f')" type="button">
                <input name="sort_g2m" value="G-M" id="sort_g2m" onclick="listrange('g','m')" type="button">
                <input name="sort_n2s" value="N-S" id="sort_n2s" onclick="listrange('n','s')" type="button">
                <input name="sort_t2z" value="T-Z" id="sort_t2z" onclick="listrange('t','z')" type="button">
            </td>
            <td style="height: 8px;" align="right">
                <input name="send_Message" value="Ok" id="send_Message" style="width: 75px;" type="submit" 
                onclick="selectAll($('ListBox2'))"></td>
        </tr>
    </tbody></table>
</div>

</div>
</form>

<script>
listrange('a','n');
Calendar.setup({
    inputField     :    "send_date",     // id of the input field
    ifFormat       :    "%Y-%m-%d %I:%M %p",      // format of the input field
    button         :    "triger1",  // trigger for the calendar (button ID)
    showsTime      :    true,
    align          :    "Br",           // alignment (defaults to "Bl")
    singleClick    :    true
});

</script>

<?php require('footer.php'); ?>
